﻿using System;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Web;
using System.Collections.Specialized;
using SyCODE.Component.DataFactory;

namespace SyCODE.Blog.Updater
{
    /// <summary>
    /// HeadInfo 更新类
    /// </summary>
    public class HeadInfo : Information
    {
        public HeadInfo(string bloger)
            : base(bloger)
        {
        }

        /// <summary>
        /// 设置用户资料
        /// </summary>
        /// <param name="name"></param>
        /// <param name="email"></param>
        public void SetUserInfo(string name, string email)
        {

            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("u", Bloger);
            nvc.Add("n", name);
            nvc.Add("e", email);

            DBAM dbam = new DBAM();
            dbam.BuildParameters("setuserinfo", nvc);

            try
            {
                dbam.Execute();
            }
            finally
            {
                dbam.Dispose();
                UserDocument.DocumentElement.SelectSingleNode("head/meta[@name='author']").Attributes["content"].Value = name;
            }
        }

        /// <summary>
        /// 设置BLOG信息
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="description">副标题</param>
        public void SetBaseInfo(string title, string description)
        {
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("u", Bloger);
            nvc.Add("t", title);
            nvc.Add("s", description);

            DBAM dbam = new DBAM();
            dbam.BuildParameters("setbloginfo", nvc);

            try
            {
                dbam.Execute();
            }
            finally
            {
                dbam.Dispose();
                UserDocument.DocumentElement.SelectSingleNode("head/title").InnerText = title;
                UserDocument.DocumentElement.SelectSingleNode("head/meta[@name='description']").Attributes["content"].Value = description;
            }

        }

        /// <summary>
        /// 设置形象图片
        /// </summary>
        /// <param name="value">图片名称</param>
        public void SetFace(string value)
        {
            DBAM dbam = new DBAM();

            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("u", Bloger);
            nvc.Add("p", HttpUtility.UrlPathEncode(value));
            dbam.BuildParameters("setphoto", nvc);

            try
            {
                dbam.Execute();
            }
            finally
            {
                dbam.Dispose();
                XmlElement authorElement = (XmlElement)UserDocument.DocumentElement.SelectSingleNode("head/meta[@name='author']");
                authorElement.SetAttribute("head", "/" + Bloger + "/face/" + HttpUtility.UrlEncode(value).Replace("+", "%20"));
            }
        }
    }
}